package com.mfangsoft.zhuangjialong.integration.product.controller;


import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.integration.product.model.BrandProductEntity;
import com.mfangsoft.zhuangjialong.integration.product.service.ProductService;

@Controller
@RequestMapping("/server")
public class ProductController {

	@Autowired
	private ProductService productService;
	
	
	@RequestMapping(value = "/addproduct", method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<String> addprdouct(@RequestBody BrandProductEntity brandProductEntity) {

		ResponseMessage<String> message = new ResponseMessage<>();

		try {
			productService.addProduct(brandProductEntity);

			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			

			return message;
		} catch (Exception e) {
			// TODO: handle exception

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}

		return message;

	}

	
	@RequestMapping(value = "/modifyproduct", method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<String> modifyProduct(@RequestBody BrandProductEntity brandProductEntity) {

		ResponseMessage<String> message = new ResponseMessage<>();

		try {
			productService.modifyProduct(brandProductEntity);

			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);

			return message;
		} catch (Exception e) {
			// TODO: handle exception

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}

		return message;

	}
	
	
	@RequestMapping(value = "/getproduct/{id}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseMessage<BrandProductEntity> getProductById(@PathVariable Long id) {

		ResponseMessage<BrandProductEntity> message = new ResponseMessage<>();

		try {
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			
			message.setResult(productService.getProductById(id));

			return message;
		} catch (Exception e) {
			// TODO: handle exception

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}

		return message;

	}
	
	
	@RequestMapping(value = "/queryproductpage", method = RequestMethod.POST)
	@ResponseBody
	public Page<Map<String, Object>> queryProductPage(@RequestBody  Page<Map<String,Object>> page){
		
	
			
			return productService.queryProductForPage(page);
		

	}
	
	
	@RequestMapping(value = "/removeproductsaleattr/{id}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseMessage<String> removeProductSaleattr(@PathVariable Long id) {

		ResponseMessage<String> message = new ResponseMessage<>();

		try {
			productService.removeProductSaleattr(id);

			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);

			return message;
		} catch (Exception e) {
			// TODO: handle exception

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}

		return message;

	}
	
	@RequestMapping(value = "/modifysaleprice/{type}/{value}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseMessage<String> modifyProduct(@PathVariable  Integer type,@PathVariable  Boolean value) {

		ResponseMessage<String> message = new ResponseMessage<>();

		try {
			productService.setProductPrice(type,value);

			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);

			return message;
		} catch (Exception e) {
			// TODO: handle exception

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}

		return message;

	}
	
	@RequestMapping(value = "/copy/{ids}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseMessage<String> copy(@PathVariable  String ids) {

		ResponseMessage<String> message = new ResponseMessage<>();

		try {
			productService.copyProduct(ids);

			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);

			return message;
		} catch (Exception e) {
			// TODO: handle exception

			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}

		return message;

	}
	
	
	
	@RequestMapping(value="/onproduct",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<String>  onBrandProduct(@RequestBody List<Long> list){
		
		
		ResponseMessage<String> message = new ResponseMessage<>();
		
		if(list==null||list.size()==0){
			
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
			
		}
		
		try {
			productService.updateBrandProduct(list, 1);
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			// TODO: handle exception
		}
		
		return message;
		
	}
	
	@RequestMapping(value="/offproduct",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<String>  offBrandProduct(@RequestBody List<Long> list){
		
		
		ResponseMessage<String> message = new ResponseMessage<>();
		
		if(list==null||list.size()==0){
			
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
			
		}
		
		try {
			   productService.updateBrandProduct(list, 0);
				
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
			
		} catch (Exception e) {
			
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			// TODO: handle exception
		}
		
		return message;
		
	}
	
//	@RequestMapping(value="/makeQRCODE",method=RequestMethod.POST)
//	@ResponseBody
//	public ResponseMessage<String>  makeQRCODE(@RequestBody BrandProductEntity brandProductEntity){
//		
//		
//		ResponseMessage<String> message = new ResponseMessage<>();
//		String qr_code = brandProductService.makeQRCODE(brandProductEntity);
//		if(qr_code!=null){
//			message.setCode(SysConstant.SUCCESS_CODE);
//			message.setMessage(SysConstant.SUCCESS_MSG);
//			message.setResult(qr_code);
//		} else {
//			message.setCode(SysConstant.FAILURE_CODE);
//			message.setMessage(SysConstant.FAILURE_MSG);
//		}
//			return message;
//	}
//	@RequestMapping(value="/makeQRCODEForList",method=RequestMethod.POST)
//	@ResponseBody
//	public ResponseMessage<Map<String, Object>>  makeQRCODE(@RequestBody List<BrandProductEntity> brandProductEntities){
//		
//		
//		ResponseMessage<Map<String, Object>> message = new ResponseMessage<>();
//		Set<Long> set = new HashSet<>();
//		for (BrandProductEntity brandProductEntity : brandProductEntities) {
//			set.add(brandProductEntity.getFactory_id());
//		}
//		Iterator<Long> iterator = set.iterator();
//		List<String> qcs = new ArrayList<>();
//		List<Map<String, Object>> failList = new ArrayList<>();
//		Map<String, Object> map = new HashMap<>();
//		while(iterator.hasNext()){
//			Long next = iterator.next();
//			List<BrandProductEntity> list = new ArrayList<>();
//			for (BrandProductEntity brandProductEntity : brandProductEntities) {
//				System.out.println(brandProductEntity.getFactory_id());
//				if(brandProductEntity.getFactory_id().intValue() == next.intValue()){
//					list.add(brandProductEntity);
//				}
//			}
//			List<String> qrcodes = brandProductService.makeQRCODEs(list, next);
//			if(qrcodes.size()>0){
//			for (String string : qrcodes) {
//				String newString = string.replaceAll(PropUtis.getValue(PropUtis.FILE_URL), PropUtis.getValue(PropUtis.FILE_PATH));
//				qcs.add(newString);
//			}
//			}else{
//				Map<String, Object> mp = new HashMap<>();
//				List<BrandModel> brands = userBrandEntityMapper.selectBrandByUserId(next);
//				if(brands.size()==1){
//					mp.put("brand_name", brands.get(0).getBrand_name());
//				}
//				mp.put("products", list);
//				failList.add(mp);
//			}
//		}
//			message.setCode(SysConstant.SUCCESS_CODE);
//			message.setMessage(SysConstant.SUCCESS_MSG);
//			if(qcs.size()>0){
//				map.put("zipPath", FileUtils.compress(qcs));
//			}else{
//				map.put("zipPath", "");
//			}
//			map.put("failList", failList);
//			message.setResult(map);
//		return message;
//	}
	
}
